<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>智能客户关系管理助手 - 产品管理</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }

        :root {
            --primary: #4361ee;
            --secondary: #3f37c9;
            --accent: #4895ef;
            --dark: #1e1e2c;
            --darker: #14141f;
            --light: #f8f9fa;
            --success: #4caf50;
            --warning: #ff9800;
            --danger: #f44336;
            --gray: #6c757d;
            --border-radius: 8px;
            --transition: all 0.3s ease;
            --product-1: #ff6b6b;
            --product-2: #4ecdc4;
            --product-3: #ffd166;
            --product-4: #6a0572;
        }

        body {
            background-color: var(--darker);
            color: var(--light);
            display: flex;
            min-height: 100vh;
        }

        /* 侧边栏样式 */
        .sidebar {
            width: 250px;
            background-color: var(--dark);
            padding: 20px 0;
            height: 100vh;
            position: fixed;
            transition: var(--transition);
            box-shadow: 0 0 15px rgba(0, 0, 0, 0.2);
            z-index: 100;
        }

        .logo-container {
            display: flex;
            align-items: center;
            padding: 0 20px 20px;
            border-bottom: 1px solid rgba(255, 255, 255, 0.1);
            margin-bottom: 20px;
        }

        .logo {
            width: 40px;
            height: 40px;
            background: linear-gradient(135deg, var(--accent), var(--primary));
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            margin-right: 10px;
        }

        .logo i {
            font-size: 20px;
        }

        .logo-text {
            font-size: 20px;
            font-weight: 700;
            background: linear-gradient(to right, var(--accent), var(--primary));
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }

        .nav-links {
            list-style: none;
            padding: 0 15px;
        }

        .nav-links li {
            margin-bottom: 5px;
        }

        .nav-links a {
            display: flex;
            align-items: center;
            padding: 12px 15px;
            text-decoration: none;
            color: rgba(255, 255, 255, 0.7);
            border-radius: var(--border-radius);
            transition: var(--transition);
        }

        .nav-links a:hover, .nav-links a.active {
            background-color: rgba(67, 97, 238, 0.2);
            color: var(--light);
        }

        .nav-links a i {
            margin-right: 10px;
            width: 20px;
            text-align: center;
        }

        /* 主内容区样式 */
        .main-content {
            flex: 1;
            margin-left: 250px;
            padding: 20px;
            transition: var(--transition);
        }

        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 30px;
            padding-bottom: 15px;
            border-bottom: 1px solid rgba(255, 255, 255, 0.1);
        }

        .page-title {
            font-size: 24px;
            font-weight: 600;
            display: flex;
            align-items: center;
        }

        .page-title i {
            margin-right: 10px;
            color: var(--accent);
        }

        .user-actions {
            display: flex;
            align-items: center;
            gap: 15px;
        }

        .user-id-input {
            display: flex;
            gap: 10px;
            align-items: center;
            background-color: rgba(255, 255, 255, 0.08);
            padding: 8px 15px;
            border-radius: 30px;
        }

        .user-id-input input {
            background: transparent;
            border: none;
            color: white;
            font-size: 14px;
            width: 120px;
        }

        .user-id-input input::placeholder {
            color: rgba(255, 255, 255, 0.6);
        }

        .user-id-input button {
            background: var(--primary);
            border: none;
            color: white;
            padding: 5px 10px;
            border-radius: 20px;
            font-size: 12px;
            cursor: pointer;
        }

        .search-container {
            display: flex;
            flex-direction: column;
        }

        .search-container label {
            font-size: 14px;
            margin-bottom: 5px;
            opacity: 0.8;
        }

        .search-box {
            position: relative;
            height: 40px;
            display: flex;
            align-items: center;
        }

        .search-box input {
            background-color: rgba(255, 255, 255, 0.08);
            border: none;
            padding: 0 15px 0 40px;
            border-radius: 30px;
            color: var(--light);
            width: 250px;
            height: 100%;
            line-height: 1;
            margin: 0;
            transition: var(--transition);
        }

        .search-box input:focus {
            outline: none;
            background-color: rgba(255, 255, 255, 0.15);
            width: 300px;
        }

        .search-box i {
            position: absolute;
            left: 15px;
            top: 50%;
            transform: translateY(-50%);
            color: rgba(255, 255, 255, 0.5);
            margin: 0;
        }

        .user-profile {
            display: flex;
            align-items: center;
            background-color: rgba(255, 255, 255, 0.08);
            padding: 8px 15px;
            border-radius: 30px;
            cursor: pointer;
            transition: var(--transition);
        }

        .user-profile:hover {
            background-color: rgba(255, 255, 255, 0.15);
        }

        .user-profile img {
            width: 32px;
            height: 32px;
            border-radius: 50%;
            margin-right: 10px;
            object-fit: cover;
        }

        /* 卡片样式 */
        .card {
            background-color: var(--dark);
            border-radius: var(--border-radius);
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
            margin-bottom: 25px;
            overflow: hidden;
        }

        .card-header {
            padding: 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            border-bottom: 1px solid rgba(255, 255, 255, 0.1);
        }

        .card-title {
            font-size: 18px;
            font-weight: 600;
        }

        .card-body {
            padding: 20px;
        }

        /* 产品管理区域 */
        .product-management {
            display: flex;
            flex-direction: column;
            gap: 20px;
        }

        .action-bar {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 15px;
        }

        .filters {
            display: flex;
            gap: 15px;
            flex-wrap: wrap;
            margin-bottom: 20px;
            background-color: rgba(255, 255, 255, 0.05);
            padding: 15px;
            border-radius: var(--border-radius);
        }

        .filter-group {
            display: flex;
            flex-direction: column;
            min-width: 200px;
        }

        .filter-group label {
            font-size: 14px;
            margin-bottom: 5px;
            opacity: 0.8;
        }

        .filter-group select, .filter-group input {
            background-color: rgba(255, 255, 255, 0.08);
            border: none;
            padding: 10px 15px;
            border-radius: var(--border-radius);
            color: var(--light);
        }

        .filter-group select:focus, .filter-group input:focus {
            outline: none;
            background-color: rgba(255, 255, 255, 0.12);
        }

        /* 产品卡片网格 */
        .products-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
            gap: 20px;
            margin-bottom: 25px;
        }

        .product-card {
            background: var(--dark);
            border-radius: var(--border-radius);
            overflow: hidden;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
            transition: var(--transition);
            display: flex;
            flex-direction: column;
            height: 100%;
        }

        .product-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 8px 25px rgba(67, 97, 238, 0.3);
        }

        .product-image {
            height: 180px;
            background-size: cover;
            background-position: center;
            position: relative;
        }

        .product-badge {
            position: absolute;
            top: 10px;
            right: 10px;
            padding: 5px 10px;
            border-radius: 20px;
            font-size: 12px;
            font-weight: 500;
            background-color: rgba(76, 175, 80, 0.2);
            color: var(--success);
        }

        .product-info {
            padding: 20px;
            flex: 1;
            display: flex;
            flex-direction: column;
        }

        .product-header {
            display: flex;
            justify-content: space-between;
            margin-bottom: 10px;
        }

        .product-name {
            font-size: 18px;
            font-weight: 600;
            color: var(--light);
        }

        .product-category {
            font-size: 14px;
            color: var(--accent);
            background: rgba(67, 97, 238, 0.1);
            padding: 3px 8px;
            border-radius: 20px;
        }

        .product-description {
            margin-bottom: 15px;
            color: rgba(255, 255, 255, 0.7);
            font-size: 14px;
            flex: 1;
        }

        .product-meta {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-top: auto;
        }

        .product-price {
            font-size: 20px;
            font-weight: 700;
            color: var(--warning);
        }

        .product-stock {
            display: flex;
            align-items: center;
            font-size: 14px;
        }

        .stock-indicator {
            width: 10px;
            height: 10px;
            border-radius: 50%;
            margin-right: 5px;
        }

        .stock-high {
            background-color: var(--success);
        }

        .stock-medium {
            background-color: var(--warning);
        }

        .stock-low {
            background-color: var(--danger);
        }

        .product-actions {
            display: flex;
            justify-content: flex-end;
            padding: 15px;
            background: rgba(255, 255, 255, 0.05);
            gap: 10px;
        }

        /* 表格样式 */
        .table-container {
            overflow-x: auto;
            border-radius: var(--border-radius);
            background-color: rgba(255, 255, 255, 0.05);
        }

        table {
            width: 100%;
            border-collapse: collapse;
            min-width: 1000px;
        }

        th, td {
            padding: 12px 15px;
            text-align: left;
            border-bottom: 1px solid rgba(255, 255, 255, 0.1);
        }

        th {
            background-color: rgba(67, 97, 238, 0.2);
            font-weight: 600;
            color: var(--accent);
        }

        tr:hover {
            background-color: rgba(255, 255, 255, 0.05);
        }

        .status-badge {
            display: inline-block;
            padding: 4px 10px;
            border-radius: 20px;
            font-size: 12px;
            font-weight: 500;
        }

        .status-active {
            background-color: rgba(76, 175, 80, 0.2);
            color: var(--success);
        }

        .status-inactive {
            background-color: rgba(244, 67, 54, 0.2);
            color: var(--danger);
        }

        .actions {
            display: flex;
            gap: 8px;
        }

        .action-btn {
            width: 30px;
            height: 30px;
            display: flex;
            align-items: center;
            justify-content: center;
            border-radius: 50%;
            border: none;
            background-color: rgba(255, 255, 255, 0.1);
            color: var(--light);
            cursor: pointer;
            transition: var(--transition);
        }

        .action-btn:hover {
            background-color: var(--accent);
        }

        .btn {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            padding: 10px 20px;
            border-radius: var(--border-radius);
            border: none;
            cursor: pointer;
            font-weight: 500;
            transition: var(--transition);
        }

        .btn i {
            margin-right: 8px;
        }

        .btn-primary {
            background-color: var(--primary);
            color: white;
        }

        .btn-primary:hover {
            background-color: var(--secondary);
        }

        .btn-success {
            background-color: var(--success);
            color: white;
        }

        .btn-warning {
            background-color: var(--warning);
            color: white;
        }

        .btn-danger {
            background-color: var(--danger);
            color: white;
        }

        .btn-outline {
            background-color: transparent;
            border: 1px solid rgba(255, 255, 255, 0.2);
            color: rgba(255, 255, 255, 0.7);
        }

        .btn-outline:hover {
            background-color: rgba(255, 255, 255, 0.1);
            color: var(--light);
        }

        .loading {
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 20px;
        }

        .spinner {
            width: 24px;
            height: 24px;
            border: 3px solid rgba(255, 255, 255, 0.3);
            border-radius: 50%;
            border-top-color: var(--accent);
            animation: spin 1s linear infinite;
            margin-right: 10px;
        }

        @keyframes spin {
            to { transform: rotate(360deg); }
        }

        /* 模态框样式 */
        .modal-overlay {
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background-color: rgba(0, 0, 0, 0.7);
            display: flex;
            align-items: center;
            justify-content: center;
            z-index: 1000;
        }

        .modal {
            background-color: var(--dark);
            width: 90%;
            max-width: 800px;
            border-radius: var(--border-radius);
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
            overflow: hidden;
        }

        .modal-header {
            padding: 20px;
            background-color: rgba(67, 97, 238, 0.2);
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .modal-title {
            font-size: 18px;
            font-weight: 600;
            color: var(--accent);
        }

        .modal-close {
            background: none;
            border: none;
            color: var(--light);
            font-size: 20px;
            cursor: pointer;
            width: 30px;
            height: 30px;
            display: flex;
            align-items: center;
            justify-content: center;
            border-radius: 50%;
            transition: var(--transition);
        }

        .modal-close:hover {
            background-color: rgba(255, 255, 255, 0.1);
        }

        .modal-body {
            padding: 20px;
            max-height: 70vh;
            overflow-y: auto;
        }

        .form-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
            gap: 20px;
        }

        .form-group {
            margin-bottom: 15px;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            font-size: 14px;
            opacity: 0.9;
        }

        .form-group input, .form-group select, .form-group textarea {
            width: 100%;
            padding: 12px 15px;
            background-color: rgba(255, 255, 255, 0.08);
            border: none;
            border-radius: var(--border-radius);
            color: var(--light);
        }

        .form-group textarea {
            height: 100px;
            resize: vertical;
        }

        .form-group input:focus, .form-group select:focus, .form-group textarea:focus {
            outline: none;
            background-color: rgba(255, 255, 255, 0.12);
        }

        .modal-footer {
            padding: 15px 20px;
            background-color: rgba(255, 255, 255, 0.05);
            display: flex;
            justify-content: flex-end;
            gap: 10px;
        }

        /* 统计卡片样式 */
        .stats-container {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 20px;
            margin-bottom: 25px;
        }

        .stat-card {
            background: var(--dark);
            border-radius: var(--border-radius);
            padding: 20px;
            display: flex;
            flex-direction: column;
            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.15);
        }

        .stat-value {
            font-size: 28px;
            font-weight: 700;
            margin-bottom: 10px;
        }

        .stat-label {
            font-size: 14px;
            color: rgba(255, 255, 255, 0.7);
        }

        .stat-icon {
            width: 50px;
            height: 50px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            margin-bottom: 15px;
            font-size: 20px;
        }

        .stat-icon.products {
            background: rgba(67, 97, 238, 0.2);
            color: var(--accent);
        }

        .stat-icon.categories {
            background: rgba(255, 107, 107, 0.2);
            color: var(--product-1);
        }

        .stat-icon.active {
            background: rgba(76, 175, 80, 0.2);
            color: var(--success);
        }

        .stat-icon.low-stock {
            background: rgba(255, 152, 0, 0.2);
            color: var(--warning);
        }

        /* 响应式设计 */
        @media (max-width: 992px) {
            .sidebar {
                width: 80px;
            }

            .logo-text, .link-text {
                display: none;
            }

            .nav-links a {
                justify-content: center;
            }

            .nav-links a i {
                margin-right: 0;
                font-size: 20px;
            }

            .main-content {
                margin-left: 80px;
            }

            .filters {
                flex-direction: column;
            }
        }

        @media (max-width: 768px) {
            .search-box input {
                width: 150px;
            }

            .search-box input:focus {
                width: 180px;
            }

            .header {
                flex-direction: column;
                align-items: flex-start;
                gap: 15px;
            }

            .user-actions {
                width: 100%;
                justify-content: space-between;
            }

            .action-bar {
                flex-direction: column;
                align-items: flex-start;
                gap: 15px;
            }

            .products-grid {
                grid-template-columns: 1fr;
            }
        }
    </style>
</head>
<body>
<!-- 侧边导航 -->
<div class="sidebar">
    <div class="logo-container">
        <div class="logo">
            <i class="fas fa-brain"></i>
        </div>
        <div class="logo-text">智能CRM</div>
    </div>

    <ul class="nav-links">
        <li><a href="/main"><i class="fas fa-home"></i> <span class="link-text">AI助手</span></a></li>
        <li><a href="/customer"><i class="fas fa-users"></i> <span class="link-text">客户管理</span></a></li>
        <li><a href="/base_dict"><i class="fas fa-book"></i> <span class="link-text">数据字典</span></a></li>
        <li><a href="/usermanage"><i class="fas fa-user-cog"></i> <span class="link-text">用户管理</span></a></li>
        <li><a href="/product" class="active"><i class="fas fa-box"></i> <span class="link-text">产品管理</span></a></li>
        <li><a href="/orderitems"><i class="fas fa-shopping-cart"></i> <span class="link-text">订单管理</span></a></li>
    </ul>
</div>

<!-- 主内容区 -->
<div class="main-content" id="app">
    <!-- 头部区域 -->
    <div class="header">
        <div class="page-title">
            <i class="fas fa-box"></i> 产品管理
        </div>

        <div class="user-actions">
            <div class="user-profile">
                <img src="https://randomuser.me/api/portraits/men/32.jpg" alt="用户头像">
                <span th:text="${session.username}"></span>
            </div>
        </div>
    </div>

    <!-- 产品管理区域 -->
    <div class="product-management">
        <!-- 统计卡片 -->
        <div class="stats-container">
            <div class="stat-card">
                <div class="stat-icon products">
                    <i class="fas fa-box"></i>
                </div>
                <div class="stat-value">{{ products.length }}</div>
                <div class="stat-label">产品总数</div>
            </div>
            <div class="stat-card">
                <div class="stat-icon categories">
                    <i class="fas fa-tags"></i>
                </div>
                <div class="stat-value">{{ categories.length }}</div>
                <div class="stat-label">产品类别</div>
            </div>
            <div class="stat-card">
                <div class="stat-icon active">
                    <i class="fas fa-check-circle"></i>
                </div>
                <div class="stat-value">{{ activeProducts }}</div>
                <div class="stat-label">上架产品</div>
            </div>
            <div class="stat-card">
                <div class="stat-icon low-stock">
                    <i class="fas fa-exclamation-triangle"></i>
                </div>
                <div class="stat-value">{{ lowStockProducts }}</div>
                <div class="stat-label">库存紧张</div>
            </div>
        </div>

        <div class="action-bar">
            <h2><i class="fas fa-boxes"></i> 产品列表</h2>
            <div>
                <button class="btn btn-primary" @click="openAddModal">
                    <i class="fas fa-plus"></i> 添加新产品
                </button>
            </div>
        </div>

        <!-- 筛选区域 -->
        <div class="filters">
            <div class="filter-group">
                <label>产品类别</label>
                <select v-model="filters.category" @change="applyFilters">
                    <option value="">全部类别</option>
                    <option v-for="category in categories" :value="category">{{ category }}</option>
                </select>
            </div>

            <div class="filter-group">
                <label>产品状态</label>
                <select v-model="filters.active" @change="applyFilters">
                    <option value="">全部状态</option>
                    <option value="active">上架</option>
                    <option value="inactive">下架</option>
                </select>
            </div>

            <div class="filter-group">
                <label>库存状态</label>
                <select v-model="filters.stock" @change="applyFilters">
                    <option value="">全部</option>
                    <option value="high">库存充足</option>
                    <option value="medium">库存中等</option>
                    <option value="low">库存紧张</option>
                </select>
            </div>

            <div class="filter-group">
                <label>价格范围</label>
                <input type="text" v-model="filters.priceRange" placeholder="例如: 100-500" @change="applyFilters">
            </div>
            <div class="search-container">
                <label>搜索：</label>
                <div class="search-box">
                    <i class="fas fa-search"></i>
                    <input type="text" v-model="searchQuery" placeholder="搜索产品..." @input="searchCustomers">
                </div>
            </div>
        </div>

        <!-- 产品表格视图 -->
        <div class="card">
            <div class="table-container">
                <table>
                    <thead>
                    <tr>
                        <th>ID</th>
                        <th>产品名称</th>
                        <th>类别</th>
                        <th>价格</th>
                        <th>状态</th>
                        <th>创建时间</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    <tr v-if="loading">
                        <td colspan="8" class="loading">
                            <div class="spinner"></div>
                            <span>加载产品数据中...</span>
                        </td>
                    </tr>
                    <tr v-else-if="filteredProducts.length === 0">
                        <td colspan="8" style="text-align: center; padding: 30px;">
                            没有找到匹配的产品
                        </td>
                    </tr>
                    <tr v-for="product in paginatedProducts" :key="product.id">
                        <td>{{ product.id }}</td>
                        <td>
                            <div style="display: flex; align-items: center; gap: 10px;">
                                <div style="width: 40px; height: 40px; border-radius: 4px; background-size: cover; background-position: center;"
                                     :style="{ backgroundImage: 'url(' + product.image + ')' }"></div>
                                {{ product.name }}
                            </div>
                        </td>
                        <td>{{ product.category }}</td>
                        <td>¥{{ product.price.toFixed(2) }}</td>
                        <td>
                                <span class="status-badge" :class="{'status-active': product.active, 'status-inactive': !product.active}">
                                    {{ product.active ? '上架' : '下架' }}
                                </span>
                        </td>
                        <td>{{ formatDate(product.created_at) }}</td>
                        <td>
                            <div class="actions">
                                <button class="action-btn" @click="openEditModal(product)">
                                    <i class="fas fa-edit"></i>
                                </button>
                                <button class="action-btn" @click="toggleProductStatus(product)">
                                    <i class="fas" :class="{'fa-toggle-on': product.active, 'fa-toggle-off': !product.active}"></i>
                                </button>
                                <button class="action-btn" @click="deleteProduct(product.id)">
                                    <i class="fas fa-trash"></i>
                                </button>
                            </div>
                        </td>
                    </tr>
                    </tbody>
                </table>
            </div>
        </div>

        <!-- 分页控件 -->
        <div class="pagination" v-if="filteredProducts.length > 0" style="display: flex; justify-content: center; align-items: center; margin-top: 20px;">
            <button class="btn btn-outline" :disabled="currentPage === 1" @click="prevPage">
                <i class="fas fa-chevron-left"></i> 上一页
            </button>
            <span style="margin: 0 15px;">第 {{ currentPage }} 页 / 共 {{ totalPages }} 页</span>
            <button class="btn btn-outline" :disabled="currentPage === totalPages" @click="nextPage">
                下一页 <i class="fas fa-chevron-right"></i>
            </button>
        </div>
    </div>

    <!-- 添加/编辑产品模态框 -->
    <div class="modal-overlay" v-if="showModal">
        <div class="modal">
            <div class="modal-header">
                <div class="modal-title">
                    <i class="fas" :class="{'fa-plus': isAdding, 'fa-edit': !isAdding}"></i>
                    {{ isAdding ? '添加新产品' : '编辑产品信息' }}
                </div>
                <button class="modal-close" @click="closeModal">
                    <i class="fas fa-times"></i>
                </button>
            </div>
            <div class="modal-body">
                <div class="form-grid">
                    <div class="form-group">
                        <label>产品名称 *</label>
                        <input type="text" v-model="currentProduct.name" placeholder="输入产品名称">
                    </div>
                    <div class="form-group">
                        <label>产品类别 *</label>
                        <select v-model="currentProduct.category">
                            <option v-for="category in categories" :value="category">{{ category }}</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label>产品价格 *</label>
                        <input type="number" v-model="currentProduct.price" placeholder="输入产品价格" min="0" step="0.01">
                    </div>
                    <div class="form-group">
                        <label>库存数量 *</label>
                        <input type="number" v-model="currentProduct.stock" placeholder="输入库存数量" min="0">
                    </div>
                    <div class="form-group">
                        <label>产品图片 URL</label>
                        <input type="text" v-model="currentProduct.image" placeholder="输入图片URL">
                    </div>
                    <div class="form-group">
                        <label>产品状态</label>
                        <select v-model="currentProduct.active">
                            <option :value="true">上架</option>
                            <option :value="false">下架</option>
                        </select>
                    </div>
                    <div class="form-group" style="grid-column: 1 / -1;">
                        <label>产品描述</label>
                        <textarea v-model="currentProduct.description" placeholder="输入产品描述"></textarea>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button class="btn btn-outline" @click="closeModal">取消</button>
                <button class="btn btn-primary" @click="saveProduct">
                    <i class="fas fa-save"></i> {{ isAdding ? '添加产品' : '保存更改' }}
                </button>
            </div>
        </div>
    </div>
</div>

<script>
    new Vue({
        el: '#app',
        data: {
            userId: 'user_' + Math.floor(Math.random() * 1000),
            searchQuery: '',
            products: [],
            filteredProducts: [],
            loading: true,
            showModal: false,
            isAdding: true,
            currentProduct: null,
            currentPage: 1,
            itemsPerPage: 8,
            viewMode: 'grid',
            filters: {
                category: '',
                active: '',
                stock: '',
                priceRange: ''
            },
            categories: ['电子产品', '家居用品', '服装', '食品', '图书', '美妆', '运动器材', '其他']
        },
        computed: {
            activeProducts() {
                return this.products.filter(p => p.active).length;
            },
            lowStockProducts() {
                return this.products.filter(p => p.stock <= 20).length;
            },
            totalPages() {
                return Math.ceil(this.filteredProducts.length / this.itemsPerPage);
            },
            paginatedProducts() {
                const start = (this.currentPage - 1) * this.itemsPerPage;
                const end = start + this.itemsPerPage;
                return this.filteredProducts.slice(start, end);
            }
        },
        watch: {
            filters: {
                deep: true,
                handler() {
                    this.applyFilters();
                }
            },
            searchQuery() {
                this.applyFilters();
            }
        },
        mounted() {
            this.fetchProducts();
        },
        methods: {
            fetchProducts() {
                this.loading = true;
                axios.get('/api/products/all')
                    .then(response => {
                        this.products = response.data.map(p => ({
                            id: p.prod_id,
                            name: p.prod_name,
                            category: p.prod_type,
                            price: p.prod_price,
                            cost: p.prod_cost,
                            description: p.prod_desc,
                            active: p.prod_status === 1,
                            stock: Math.floor(Math.random() * 100),
                            image: 'https://via.placeholder.com/100',
                            created_at: p.create_time
                        }));
                        this.applyFilters();
                        this.loading = false;
                    })
                    .catch(error => {
                        console.error('获取产品失败:', error);
                        alert('获取产品数据失败');
                        this.loading = false;
                    });
            },
            applyFilters() {
                let result = [...this.products];

                if (this.searchQuery) {
                    const query = this.searchQuery.toLowerCase();
                    result = result.filter(product =>
                        product.name.toLowerCase().includes(query) ||
                        product.description.toLowerCase().includes(query) ||
                        product.category.toLowerCase().includes(query)
                    );
                }

                if (this.filters.category) {
                    result = result.filter(p => p.category === this.filters.category);
                }

                if (this.filters.active === 'active') {
                    result = result.filter(p => p.active);
                } else if (this.filters.active === 'inactive') {
                    result = result.filter(p => !p.active);
                }

                if (this.filters.stock === 'high') {
                    result = result.filter(p => p.stock > 50);
                } else if (this.filters.stock === 'medium') {
                    result = result.filter(p => p.stock > 20 && p.stock <= 50);
                } else if (this.filters.stock === 'low') {
                    result = result.filter(p => p.stock <= 20);
                }

                if (this.filters.priceRange) {
                    const [min, max] = this.filters.priceRange.split('-').map(Number);
                    if (!isNaN(min) && !isNaN(max)) {
                        result = result.filter(p => p.price >= min && p.price <= max);
                    }
                }

                this.filteredProducts = result;
                this.currentPage = 1;
            },
            openAddModal() {
                this.currentProduct = this.getEmptyProduct();
                this.isAdding = true;
                this.showModal = true;
            },
            openEditModal(product) {
                this.currentProduct = { ...product };
                this.isAdding = false;
                this.showModal = true;
            },
            closeModal() {
                this.showModal = false;
            },
            saveProduct() {
                if (!this.currentProduct.name?.trim()) {
                    alert('产品名称不能为空');
                    return;
                }
                if (!this.currentProduct.category) {
                    alert('请选择产品类别');
                    return;
                }
                if (!this.currentProduct.price || this.currentProduct.price <= 0) {
                    alert('请输入有效的产品价格');
                    return;
                }

                const url = this.isAdding ? '/api/products' : `/api/products/${this.currentProduct.id}`;
                const method = this.isAdding ? 'post' : 'put';
                const payload = {
                    prod_id: this.currentProduct.id,
                    prod_name: this.currentProduct.name,
                    prod_type: this.currentProduct.category,
                    prod_price: this.currentProduct.price,
                    prod_cost: this.currentProduct.cost || 0,
                    prod_desc: this.currentProduct.description,
                    prod_status: this.currentProduct.active ? 1 : 0,
                    create_time: this.currentProduct.created_at
                };

                axios[method](url, payload)
                    .then(response => {
                        const savedProduct = response.data;
                        const mappedProduct = {
                            id: savedProduct.prod_id,
                            name: savedProduct.prod_name,
                            category: savedProduct.prod_type,
                            price: savedProduct.prod_price,
                            cost: savedProduct.prod_cost,
                            description: savedProduct.prod_desc,
                            active: savedProduct.prod_status === 1,
                            stock: this.currentProduct.stock,
                            image: this.currentProduct.image,
                            created_at: savedProduct.create_time
                        };

                        if (this.isAdding) {
                            this.products.unshift(mappedProduct);
                        } else {
                            const index = this.products.findIndex(p => p.id === mappedProduct.id);
                            if (index !== -1) Vue.set(this.products, index, mappedProduct);
                        }
                        this.applyFilters();
                        this.showModal = false;
                        alert(`产品 ${mappedProduct.name} 已${this.isAdding ? '添加' : '更新'}！`);
                    })
                    .catch(error => {
                        console.error('保存失败:', error);
                        alert('操作失败: ' + (error.response?.data?.message || '请重试'));
                    });
            },
            deleteProduct(id) {
                if (!confirm('确定要删除此产品吗？')) return;
                axios.delete(`/api/products/${id}`)
                    .then(() => {
                        const index = this.products.findIndex(p => p.id === id);
                        if (index !== -1) {
                            this.products.splice(index, 1);
                            if (this.paginatedProducts.length === 0 && this.currentPage > 1) {
                                this.currentPage--;
                            }
                            this.applyFilters();
                        }
                    })
                    .catch(error => {
                        console.error('删除失败:', error);
                        alert('删除失败: ' + (error.response?.data?.message || ''));
                    });
            },
            toggleProductStatus(product) {
                const updatedProduct = { ...product, active: !product.active };
                const payload = {
                    prod_id: updatedProduct.id,
                    prod_name: updatedProduct.name,
                    prod_type: updatedProduct.category,
                    prod_price: updatedProduct.price,
                    prod_cost: updatedProduct.cost,
                    prod_desc: updatedProduct.description,
                    prod_status: updatedProduct.active ? 1 : 0,
                    create_time: updatedProduct.created_at
                };
                axios.put(`/api/products/${product.id}`, payload)
                    .then(() => {
                        product.active = !product.active;
                        this.applyFilters();
                        alert(`产品 ${product.name} 已${product.active ? '上架' : '下架'}！`);
                    })
                    .catch(error => {
                        console.error('状态更新失败:', error);
                        alert('状态更新失败');
                    });
            },
            getEmptyProduct() {
                return {
                    id: 0,
                    name: '',
                    category: '',
                    price: 0,
                    cost: 0,
                    stock: 0,
                    description: '',
                    image: 'https://via.placeholder.com/100',
                    active: true,
                    created_at: new Date().toISOString()
                };
            },
            nextPage() {
                if (this.currentPage < this.totalPages) {
                    this.currentPage++;
                }
            },
            prevPage() {
                if (this.currentPage > 1) {
                    this.currentPage--;
                }
            },
            goToPage(page) {
                if (page >= 1 && page <= this.totalPages) {
                    this.currentPage = page;
                }
            },
            formatDate(date) {
                return new Date(date).toLocaleDateString('zh-CN');
            }
        }
    });
</script>

</body>
</html>